package com.xuyh.pdf.utils;

import cn.hutool.core.io.IoUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * @author xuyh
 *
 * 下载爱课程中的pdf
 *
 * 获取课程数据
 *
 * JSON.stringify(Array.from(document.querySelectorAll('ul.chapter-body-l-in a[data-type="ppt"]')).map(item=>{return {title: item.dataset.title,url:item.dataset.url}}))
 */
public class DownloadPdf {
    private final static String path = "c:\\xuyh\\1\\";

    public static void main(String[] args) throws FileNotFoundException {
        String json = "[{\"title\":\"软件工程概论\",\"url\":\"https://resa.icourses.cn/share/process20/pdf/2018/4/21/0/d838a9ef-ca54-4b15-8d22-bf0b5fd440fc.pdf\"},{\"title\":\"软件工程探讨\",\"url\":\"https://resa.icourses.cn/share/process20/pdf/2018/4/21/0/f3c9b7bf-73c0-41ab-b817-8deb784f5b65.pdf\"},{\"title\":\"软件工程课程介绍\",\"url\":\"https://resa.icourses.cn/share/process20/pdf/2018/4/26/0/b21b712f-a551-464c-ae0e-85560d04044b.pdf\"},{\"title\":\"软件生存周期过程\",\"url\":\"https://resa.icourses.cn/share/process20/pdf/2018/4/21/0/f6712281-2310-49ac-92c4-848c924653bd.pdf\"},{\"title\":\"软件生存周期模型\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/19/46f13157-a320-4341-9ff1-8403050dcd1e.pdf\"},{\"title\":\"软件需求与软件需求规约\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/18/6af291f0-d7cc-47c3-8c9f-fca03e8b37d2.pdf\"},{\"title\":\"结构化分析方法\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/24/6/0bd58794-c09c-41b0-a1d3-a21b55b48657.pdf\"},{\"title\":\"结构化设计方法-总体设计\",\"url\":\"https://resa.icourses.cn/share/process20/pdf/2018/4/21/0/54bf8d70-7f8d-4db2-9128-a7a7339005e1.pdf\"},{\"title\":\"结构化设计方法-详细设计\",\"url\":\"https://resa.icourses.cn/share/process20/pdf/2018/4/21/0/0f33b22c-be8c-4875-875e-9ca3e6918508.pdf\"},{\"title\":\"敏捷软件开发\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/18/df12348a-fe6e-4654-a8f3-ee8b2fc2688e.pdf\"},{\"title\":\"面向对象方法简介\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/19/90d96c6f-0fe8-489d-92eb-031f7f4dafdf.pdf\"},{\"title\":\"UML描述客体及客体关系的术语-第一部分\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/18/1e105ec5-6dd6-45ea-987d-93e46f5cdea2.pdf\"},{\"title\":\"UML描述客体及客体关系的术语-第二部分\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/18/65c28616-c467-4ced-957a-7e3a114ee6e9.pdf\"},{\"title\":\"UML模型表达工具\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/18/04bb8f84-35f2-4477-999a-869eeb8356d4.pdf\"},{\"title\":\"面向对象分析\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/18/35b4a913-6e21-4219-b1ba-5d827b2d4c90.pdf\"},{\"title\":\"面向对象设计\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/18/db356c06-2e62-4d1c-8b75-a470592af695.pdf\"},{\"title\":\"面向对象编程\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/18/c7349f65-1c08-4395-a9dc-8a52bde5b81c.pdf\"},{\"title\":\"软件测试技术\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/18/9eca7adb-ce7a-4ed9-9d4d-3d5c571a71f3.pdf\"},{\"title\":\"软件测试步骤和测试技术应用\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/18/213c52f3-dbf7-47af-bb8a-8184f56eaf1b.pdf\"},{\"title\":\"软件项目管理简介\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/19/203a96a3-50cf-4672-a253-d4341e409c9c.pdf\"},{\"title\":\"CMM和ISO9000\",\"url\":\"https://resa.icourses.cn/share/process21/pdf/2018/4/18/19/288d7a7e-1321-4619-a998-1d7a4d679c13.pdf\"},{\"title\":\"互联网软件技术\",\"url\":\"https://resa.icourses.cn/share/process20/pdf/2018/4/21/0/094f029a-6b10-4a8f-9f07-0f5127bc8e8e.pdf\"},{\"title\":\"软件开发工具与环境\",\"url\":\"https://resa.icourses.cn/share/process20/pdf/2018/4/21/0/42605f6e-4452-43b7-abe7-709dadac989c.pdf\"},{\"title\":\"课程实践介绍\",\"url\":\"https://resa.icourses.cn/share/process20/pdf/2018/4/21/0/9d9ff9ac-2a37-4928-8b98-d0cb12f88025.pdf\"},{\"title\":\"实习项目背景介绍\",\"url\":\"https://resa.icourses.cn/share/process20/pdf/2018/4/21/0/4a74842e-d1aa-4179-94e0-8a6d85cd3f16.pdf\"},{\"title\":\"课程实践工具和技术介绍\",\"url\":\"https://resa.icourses.cn/share/process20/pdf/2018/4/21/0/91cd6ce1-d174-43df-b7cb-091a83f54ee2.pdf\"}]";
        JSONArray jsonArray = JSONUtil.parseArray(json);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject item = jsonArray.getJSONObject(i);
            String title = item.getStr("title");
            String url = item.getStr("url");

            byte[] bytes = HttpUtil.createGet(url)
                    // 必要，勿删
                    .header("Referer", "https://www.icourses.cn/")
                    // 必要，勿删
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0")
                    .execute(true).bodyBytes();
            IoUtil.write(new FileOutputStream(new File(path, i + ". " + title + ".pdf")), true, bytes);
        }
    }
}
